package com.kkd.kkdapp.adapter;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kkd.kkdapp.Environment;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.LivePlayerActivity;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.LiveHomeDataLive;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

public class LiveHomeListAdapter extends BaseAdapter {
	private Context mContext;
	private List<LiveHomeDataLive> mList;
	private LayoutInflater inflater;
	private SimpleDateFormat sdf;

	public LiveHomeListAdapter(Context context, List<LiveHomeDataLive> list) {
		mContext = context;
		mList = list;
		inflater = LayoutInflater.from(mContext);
		sdf = new SimpleDateFormat("HH:mm");
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_listview_livehome,
					parent, false);
			holder.tv_state_top = (TextView) convertView
					.findViewById(R.id.tv_state_top);
			holder.iv_content = (ImageView) convertView
					.findViewById(R.id.iv_content);
			holder.color_roundView = (ImageView) convertView
					.findViewById(R.id.color_roundView);
			holder.tv_state_content = (TextView) convertView
					.findViewById(R.id.tv_state_content);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_summary = (TextView) convertView
					.findViewById(R.id.tv_summary);
			holder.tv_num_seeing = (TextView) convertView
					.findViewById(R.id.tv_num_seeing);
			holder.toDetail = convertView.findViewById(R.id.toDetail);
			holder.share = convertView.findViewById(R.id.share);
			holder.line_bottom = convertView.findViewById(R.id.line_bottom);
			holder.over_flag = convertView.findViewById(R.id.over_flag);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ImageLoader.getInstance().displayImage(
				mList.get(position).getCoverUrl(), holder.iv_content,
				R.drawable.home_ad);
		holder.tv_title.setText(mList.get(position).getVideoName());
		holder.tv_summary.setText(mList.get(position).getProgramName()
				+ mList.get(position).getVideoCode());
		if (mList.get(position).isLiving()) {
			holder.tv_num_seeing.setVisibility(View.VISIBLE);
			holder.share.setVisibility(View.VISIBLE);
			holder.color_roundView.setVisibility(View.VISIBLE);
			holder.tv_state_top.setText("正在直播");
			holder.tv_state_content.setText("正在直播");
			holder.tv_num_seeing.setText("共"
					+ mList.get(position).getPlayCount() + "人观看");
		} else {
			holder.tv_num_seeing.setVisibility(View.GONE);
			holder.share.setVisibility(View.GONE);
			holder.color_roundView.setVisibility(View.GONE);
			holder.tv_state_top.setText("即将开播");
			holder.tv_state_content.setText(sdf.format(new Date(mList.get(
					position).getBeginTime()))
					+ "开始");
			holder.tv_num_seeing.setText("");
		}

		if (mList.get(position).getVideoStatus() == 30) { // 直播结束，显示蒙板
			holder.over_flag.setVisibility(View.VISIBLE);
		} else {
			holder.over_flag.setVisibility(View.GONE);
		}

		// 跳转到详情页
		holder.toDetail.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (mList.get(position).isLiving()
						&& mList.get(position).getVideoStatus() != 30) {
					Intent intent = new Intent(mContext,
							LivePlayerActivity.class);
					intent.putExtra("LiveHomeDataLive", mList.get(position));
					mContext.startActivity(intent);
				}
			}
		});
		// 分享
		holder.share.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (mList.get(position).isLiving()) {
					if (mList.get(position).getShareAuth() == 10) {
						PreferenceUtils.getInstance().setH5Entrance(mContext,
								"视频分享");
						((BaseActivity) mContext).toShareURL(mContext,
								Environment.getH5Url() + "livedetail.html?vid="
										+ mList.get(position).getVid()
										+ "&avatarCnt=6", "我正在快快贷直播间看“"
										+ mList.get(position).getVideoName()
										+ "”,一起来吧~", "", mList.get(position)
										.getCoverUrl());
					} else {
						AppUtil.makeToastForCustomer(mContext, "该视频不支持分享");
					}
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		ImageView iv_content, color_roundView;
		View share, line_bottom, toDetail, over_flag;
		TextView tv_state_top, tv_state_content, tv_title, tv_summary,
				tv_num_seeing;
	}
}
